package com.wxyzz.cem.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.wxyzz.cem.mapper.PaymentProjectMapper;
import com.wxyzz.cem.mapper.UserMapper;
import com.wxyzz.cem.pojo.PaymentProject;
import com.wxyzz.cem.pojo.PaymentRecord;
import com.wxyzz.cem.mapper.PaymentRecordMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wxyzz.cem.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * <p>
 * 缴费记录 服务实现类
 * </p>
 *
 * @author xc
 * @since 2024-07-04
 */
@Service
public class PaymentRecordService extends ServiceImpl<PaymentRecordMapper, PaymentRecord> implements IService<PaymentRecord> {
    @Autowired
    private PaymentRecordMapper paymentRecordMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PaymentProjectMapper paymentProjectMapper;
    public Page<PaymentRecord> getPaymentRecordByClassId(Integer page, Integer size, Integer classId) {
        QueryWrapper<PaymentRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("class_id",classId);
        Page<PaymentRecord> p = new Page<>(page, size);
        Page<PaymentRecord> paymentRecordPage = paymentRecordMapper.selectPage(p,queryWrapper);
        List<PaymentRecord> paymentRecords = paymentRecordPage.getRecords();
        for(PaymentRecord paymentRecord : paymentRecords){
            int userId = paymentRecord.getUserId();
            int paymentProjectId = paymentRecord.getPaymentProjectId();
            User user = userMapper.selectById(userId);
            PaymentProject paymentProject = paymentProjectMapper.selectById(paymentProjectId);
            paymentRecord.setUser(user);
            paymentRecord.setPaymentProject(paymentProject);
        }
        return paymentRecordPage;
    }

    public Page<PaymentRecord> getPaymentRecordByUserId(Integer page, Integer size, Integer userId) {
        QueryWrapper<PaymentRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        Page<PaymentRecord> p = new Page<>(page, size);
        Page<PaymentRecord> paymentRecordPage =paymentRecordMapper.selectPage(p,queryWrapper);
        List<PaymentRecord> paymentRecords = paymentRecordPage.getRecords();
        for(PaymentRecord paymentRecord : paymentRecords){
            int paymentProjectId = paymentRecord.getPaymentProjectId();
            PaymentProject paymentProject = paymentProjectMapper.selectById(paymentProjectId);
            paymentRecord.setPaymentProject(paymentProject);
        }
        return paymentRecordPage;
    }

    public Boolean isPaid(Integer paymentProjectId, Integer userId) {
        LambdaQueryWrapper<PaymentRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaymentRecord::getPaymentProjectId,paymentProjectId)
                .eq(PaymentRecord::getUserId,userId);
        return paymentRecordMapper.selectOne(queryWrapper) != null;
    }
}
